package cn.ardy.common.util;

import com.qunar.tts.common.json.JsonEscapeHtmlSerial;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;

public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();
    private static ObjectMapper escapeHtmlObjectMapper = new ObjectMapper();

    static {
        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        objectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);

        escapeHtmlObjectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        escapeHtmlObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        escapeHtmlObjectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        escapeHtmlObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        escapeHtmlObjectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true);
        escapeHtmlObjectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);
        JsonEscapeHtmlSerial jhs = new JsonEscapeHtmlSerial();
        SimpleModule escapeHtmlModule = new SimpleModule("EscapeHtmlModule", new Version(1, 0, 0, null));
        escapeHtmlModule.addSerializer(String.class, jhs);
        escapeHtmlObjectMapper.registerModule(escapeHtmlModule);
    }

    public static ObjectMapper getObjectMapperInstance() {
        return objectMapper;
    }

    public static ObjectMapper getEscapeHtmlObjectMapperInstance() {
        return escapeHtmlObjectMapper;
    }

}